package com.lifesaftypower.adapter;

import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.lifesaftypower.activity.R;
import com.lifesaftypower.entity.Product;

public class ProductSectionAdapter implements ExpandableListAdapter {
	private List<List<Product>> _items;
	private Context _context;
	
	public ProductSectionAdapter(Context context, List<List<Product>> items) {
		super();
		_context = context;
		_items = items;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return _items.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		Product product = (Product)getChild(groupPosition, childPosition);
		LayoutInflater vi = (LayoutInflater) _context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		View v = vi.inflate(R.layout.product_list_item, null);
		
		TextView tvName = (TextView)v.findViewById(R.id.tvProductName);
		tvName.setText(product.getName());
		TextView tvModel = (TextView)v.findViewById(R.id.tvProductModel);
		tvModel.setText(product.getModel());
		
		return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return _items.get(groupPosition).size();
	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return _items.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return _items.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		List<Product> productGroup = (List<Product>)getGroup(groupPosition);
		LayoutInflater vi = (LayoutInflater) _context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		View v = vi.inflate(R.layout.product_group, null);
		
		TextView tv = (TextView)v.findViewById(R.id.tvMarketName);
		tv.setText(productGroup.get(0).getMarketSector());
		return v;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public boolean isEmpty() {
		return _items == null || _items.size() == 0;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		
		
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	

	/*
	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		Log.i("Product Section", Integer.toString(position));
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.product_list_item, null);

			List<Product> secProducts = _items.get(position);
			if (secProducts != null && secProducts.size() > 0) {
				TextView tv = (TextView) v.findViewById(R.id.tvSectionHeader);
				tv.setText(secProducts.get(0).getMarketSector());
				ListView lv = (ListView)v.findViewById(R.id.lvSectionProduct);
				lv.setAdapter(new ProductAdapter(this.getContext(),R.layout.product_detail_list_item, _items.get(position)));
			}

		}

		return v;
	}
	*/

}
